/***********************************************************************
 * Module:  Band.java
 * Author:  juny
 * Created: 2006年7月13日 13:53:05
 * Purpose: Defines the Class Band
 ***********************************************************************/

package com.powerunion.datacollection.report.excelreport.impl;

import com.powerunion.datacollection.report.excelreport.base.BaseBand;
import com.powerunion.datacollection.report.excelreport.base.ITempletContainer;
import com.powerunion.datacollection.report.excelreport.base.Parameter;
import com.powerunion.datacollection.report.excelreport.datasource.IDataSource;
import com.powerunion.datacollection.report.excelreport.util.AnalyseTempletTool;

import jxl.CellView;
import jxl.write.WritableSheet;
import jxl.write.biff.RowsExceededException;


/**
 * 基本带类，带是描述一段区域，该区域中的所有元素都会根据带定义的数据源的记录数进行重复输出。
 * @author juny
 */
public class Band extends BaseBand {
    /** 
     * @param container
     */
    public Band(ITempletContainer container) {
        super(container);
    }
    
    /*
     * @see excel.report.util.IElementWriter#setElementTempletValue(java.lang.String)
     */
    public void setElementTempletValue(String value) {
        String name = AnalyseTempletTool.getPorperty(
                						value, 
                						AnalyseTempletTool.PROPERTY_NAME
                	  );
        this.setName(name);
        dsName = AnalyseTempletTool.getPorperty(
                		value,
                		AnalyseTempletTool.PROPERTY_DATASOURCE
                	);
        setHearder();
    }
    
    /**
     * 输出带的尾部区数据
     * @param param	参数对象
     * @throws Exception
     */
    public void writeDetail(Parameter param) throws Exception{
        //取得数据源
        IDataSource ds = (IDataSource)param.dataSources.get(dsName);
        if(null == ds){
            throw new Exception("Couldn't find datasource instance, ds=" + dsName);
        }
        //检索数据源数据
        ds.queryData();
        if(ds.size() > 0){
        	int bodyBegin = param.curRow;
        	
            do{
                this.writeBody(param);
                ds.next();
            }while(ds.hasNext());
            
            int bodyEnd = param.curRow - 1;
            if(bodyBegin < bodyEnd){
            	if(null != this.getProperty()){
            		this.mergeColumn(bodyBegin, bodyEnd, param);
            	}
            }
        }
    }
    
    /**
     * 在当前报表中插入一个空行。
     * @param sheet
     * @param sourceRow
     * @param destRow
     * @throws RowsExceededException
     */
    public void copyEmptyRow(WritableSheet sheet, int sourceRow, int destRow) 
						throws RowsExceededException{
		//拷贝行高度
		CellView rowView = sheet.getRowView(sourceRow);
		sheet.insertRow(destRow);
		sheet.setRowView(destRow, rowView.getSize());
	}
    
    private String dsName = null;

    /* (non-Javadoc)
     * @see excel.report.util.ITempletContainer#writeData(excel.report.util.Parameter)
     */
    public boolean write(Parameter param) throws Exception {
        //记录写数据时的开始行
        this.realBeginRow = param.curRow;
        //写组头数据
        this.writeHead(param);
        //写组数据
        this.writeDetail(param);
        //写组尾数据
        this.writeFoot(param);
        //记录写入数据的总行数
        this.realRowCount = param.curRow - this.realBeginRow;
        
        return true;
    }
    
    public String toString(){
        return "head{" + this.headTemplet.toString()+
        		"\n}\n   detail{" + this.detailTemplet.toString() + 
        		"\n}\n   bottom{" + this.bottomTemplet.toString() + "}\n";
    }
}